package com.test.workdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.test.workdemo.ex.ServiceException;
import com.test.workdemo.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.workdemo.dao.CeshiTableMapper;
import com.test.workdemo.pojo.entity.CeshiTable;
import com.test.workdemo.service.CeshiTableService;

@Service
@Slf4j
public class CeshiTableServiceImpl extends ServiceImpl<CeshiTableMapper, CeshiTable> implements CeshiTableService {

    @Autowired
    private CeshiTableMapper ceshiTableMapper;


    @Override
    public int mybatisplusInsert() {
        CeshiTable ceshiTable = new CeshiTable();
        ceshiTable.setName("plus");
        ceshiTable.setAge(100L);
        int num = ceshiTableMapper.insert(ceshiTable);


        if (num>0){
            return num;
        }else {
            throw  new ServiceException(ServiceCode.ERROR_BAD_REQUEST,"添加失败");
        }
    }

    @Override
    public CeshiTable mybatisplusSelect() {
        LambdaQueryWrapper<CeshiTable> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CeshiTable::getName,"as");
        queryWrapper.eq(CeshiTable::getAge,11L);
        //根据多条件查询单条数据
        CeshiTable ceshiTable = ceshiTableMapper.selectOne(queryWrapper);

        return ceshiTable;
    }

    @Override
    public List<CeshiTable> mybatisplusSelectList() {
        LambdaQueryWrapper<CeshiTable> queryWrapper = new LambdaQueryWrapper();
        //in查询
        queryWrapper.in(CeshiTable::getAge, Arrays.asList(10L,11L,12L));
        //like查询
        queryWrapper.like(CeshiTable::getName,"as");
        //降序排序
        queryWrapper.orderByDesc(CeshiTable::getId);
        //升序排序
        queryWrapper.orderByAsc(CeshiTable::getId);
        //等值查询
        queryWrapper.eq(CeshiTable::getId,10L);
        //不等于查询
        queryWrapper.ne(CeshiTable::getId,11L);
        //大于查询
        queryWrapper.gt(CeshiTable::getAge,33);
        //大于等于查询
        queryWrapper.ge(CeshiTable::getAge,33);
        //小于查询
        queryWrapper.lt(CeshiTable::getAge,22);
        //小于等于查询
        queryWrapper.le(CeshiTable::getAge,22);
        //判断空值
        queryWrapper.isNull(CeshiTable::getName);
        //判断不为空
        queryWrapper.isNotNull(CeshiTable::getAge);




        //多条件查询
        //queryWrapper.like(CeshiTable::getName, "as").eq(CeshiTable::getId, 17L).orderByDesc(CeshiTable::getId);

        List<CeshiTable> ceshiTables = ceshiTableMapper.selectList(queryWrapper);

        return ceshiTables;
    }

    @Override
    public Page<CeshiTable> getPageList(Integer pageNo, Integer pageSize) {
        Page<CeshiTable> page = new Page<>(pageNo,pageSize);
        LambdaQueryWrapper<CeshiTable> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(CeshiTable::getName,"a");
        Page<CeshiTable> ceshiTablePage = ceshiTableMapper.selectPage(page, queryWrapper);

        return ceshiTablePage;
    }


}
